{extend name='public/content'}

{block name="title"}
	商品详情
{/block}

{block name="body"}
		
		<div class="layui-container">  
			<div class="layui-row">
			    <div class="layui-col-md6">
			     	{$goods.name}
			    </div>
			    <div class="layui-col-md6">
			      <img src="{$goods.logo}" style="width: 100px;height: 100px">
			    </div>
			</div>
		</div>
		
			<div>库存：<span class="kucun">{$stock}</span></div>
			<div>价格：<span class="price"></span></div>
			{$goods.description}
	
			{if condition="($goods.is_spec == 0)"} 
					没有属性
					库存：{$goodstoresock.stock}
					价格：{$goodspec.price}
					<input type="hidden" name="goods_spec_detail_id" value="{$goodspec.id}">
					<input type="hidden" name="store_goods_stock_id" value="{$goodstoresock.id}">
					<div>价格：<span class="price" >{$goodspec.price}</span></div>
					<input type="hidden" name="goodsprice" value="{$goodspec.price}">	
			{else /}
					
					<input type="hidden" name="store_goods_stock_id">
					<input type="hidden" name="goods_spec_detail_id" value="">
					<div>价格：<span class="price"></span></div>
					<input type="hidden" name="goodsprice" value="">
				{foreach($goodspace as $v)}
				
				 <input type="hidden" 
						id="spec_id{$v.id}" class="shuxinxuanze" name="spec_id{$v.id}" 
						data-shuxin="{$v.specname.name}" value=''>				
				<div class="" style="color: red" data-specid="">{$v.specname.name}</div>
				<div>
					{foreach($v.spec as $vv}		
						<button class="dataoption" data-inputid="spec_id{$v.id}" data-option="{$vv.id}">{$vv.detail}</button>
					{/foreach}
				</div>
			 {/foreach}

			{/if}


	<input type="hidden" name="goods_spec_detaile">	

	<div class="price"></div>
		<div class="sumclick layui-btn" data-type="-">-</div>
		<input type="num" name="sum" value="1">
		<div class="sumclick layui-btn" data-type="+">+</div>
		<input type="hidden" name="goods_id" value="{$goods.id}">	
		<input type="hidden" name="storid" value="{$storeid}">
		<button id="colegoodid" data-id='{$goods.id}'>加入收藏</button>
		<button id="addusecar" data-id='{$goods.id}'>加入购物车</button>
		<button id="orderprice" data-id='{$goods.id}'>立即购买</button>
		<div><?php echo htmlspecialchars_decode($goods['detail']); ?></div>
	<style type="text/css">
		.red{
			color: red;
		}
	</style>
		
{/block}


{block name="script"}
 	<script type="text/javascript">
 		
 		var url = {
 			user :          "{:url('index/user/index')}",
 			collegood:      "{:url('index/order/collegood')}?id={$goods.id}&store_id={$storeid}",
 			addusecar :     "{:url('index/order/addusecar')}?id={$goods.id}",
 			orderpre :      "{:url('index/order/preindex')}",
 			goodproajax :   "{:url('index/index/getgoodsajax')}"
 		}
 		var is_spec = "{$goods.is_spec}";
 		//最后的商品规格
 		var goodsid = "{$goods.id}";
 		var specid = '-1';
 		require(['jquery'], function () {
 			$('.sumclick').click(function(){
 				var that = $(this);
 				var num = parseInt($('input[name=sum]').val());
 				if(that.data("type")=="-"){
 					if(num<0 || num==0){
 						$('input[name=sum]').val(0)
 					}else{
 						$('input[name=sum]').val(num-1)
 					}
 				}
 				if(that.data("type")=="+"){
 					$('input[name=sum]').val(num+1)
 				}
 			})
 		 	
 		 	//商品收藏
 		 	$('#colegoodid').click(function(){
 		 		$.ajax({
 		 			url: url.collegood,
 		 			type: 'GET',
 		 			dataType: 'json',
 		 			data: {}
 		 		})
 		 		.done(function($result) {
 		 			if($result.status=='9'){
 		 				window.location.href=url.user;
 		 			}
 		 			if($result.status=='5'){
 		 				alert('取消收藏');
 		 			}
 		 			if($result.status=='1'){
 		 				alert('收藏成功');
 		 			}
 		 		})
 		 		.fail(function() {
 		 			console.log("error");
 		 		})
 		 		.always(function() {
 		 			console.log("complete");
 		 		});
 		 	})
 		 	
 		 	//有无属性的两种商品 第一种无属性
 		 	{if condition="($goods.is_spec == 0)"}
 		 			$('#addusecar').click(function(){
					let goods_spec_detail_id = $("input[name=goods_spec_detail_id]").val();
					let store_goods_stock_id = $("input[name=store_goods_stock_id]").val();
					let goods_id 			 = $("input[name=goods_id]").val();
					let sum 				 = parseInt($('input[name=sum]').val());
					let storeid 			 = $('input[name=storid]').val();
					let goods_price 		 = $('input[name=goodsprice]').val();
					let data ={
						storeid:storeid,
						sum:sum,
						goods_id:goods_id,
						goods_spec_detail_id:goods_spec_detail_id,
						store_goods_stock_id:store_goods_stock_id,
						goods_price:goods_price
					}

					$.ajax({
	 		 			url: url.addusecar,
	 		 			type: 'GET',
	 		 			dataType: 'json',
	 		 			data: data,
	 		 		})
	 		 		.done(function($result) {
	 		 			if($result.status=='9'){
	 		 				window.location.href=url.user;
	 		 			}
	 		 			alert("成功加入购物车");
	 		 			console.log($result);	
	 		 		})
				})	
			{else /} 	
				//第二种无属性
	 		 	$('#addusecar').click(function(){
	 		 		
	 		 		var shuxin =[];
	 		 		var dis =0;
	 		 		$('.shuxinxuanze').each(function(index, elem) {
	 		 			if($(elem).val()==null || $(elem).val()==''){
	 		 				alert("请选择"+$(elem).data('shuxin'));
	 		 				//console.dir(1);
							//dis =1;
	 		 				return false ;
	 		 			}
	 		 			shuxin.push($(elem).val());
	 		 		}) 
	 		 		let goods_spec_detail_id = $("input[name=goods_spec_detail_id]").val();
					let store_goods_stock_id = $("input[name=store_goods_stock_id]").val();
					let goods_id 			 = $("input[name=goods_id]").val();
					let sum 				 = parseInt($('input[name=sum]').val());
					let storeid 			 =$('input[name=storid]').val();
					let goods_price 				 =$('input[name=goodsprice]').val();

					let data ={
						storeid:storeid,
						sum:sum,
						goods_id:goods_id,
						goods_spec_detail_id:goods_spec_detail_id,
						store_goods_stock_id:store_goods_stock_id,
						goods_price:goods_price

					}
					//console.log(data);return;
	 		 		$.ajax({
	 		 			url: url.addusecar,
	 		 			type: 'GET',
	 		 			dataType: 'json',
	 		 			data: data,
	 		 		})
	 		 		.done(function($result) {
	 		 			if($result.status=='9'){
	 		 				window.location.href=url.user;
	 		 			}
	 		 			alert("成功加入购物车");
	 		 			console.log($result);	
	 		 		})
	 		 		.fail(function() {
	 		 			console.log("error");
	 		 		})
	 		 		.always(function() {
	 		 			console.log("complete");
	 		 		});
	 		 	})

			{/if}


 		    //选择    选择需要重新去做
 		    $('.dataoption').click(function(){
 		    	var inpuid = $(this).data('inputid');
 		    	var optionid = $(this).data('option');
 		    	//alert(inpuid);alert(optionid);
 		    	$('#'+inpuid).val(optionid);

 		    	$(this).siblings().each(function(index, el) {
 		    		if($(el).hasClass("red")){
 		    			$(el).removeClass('red')
 		    		}
 		    	}); 
 		    	$(this).addClass('red') ;
 		    	let shuxin=[];
 		    	$('.shuxinxuanze').each(function(index, elem) {
 		 			if($(elem).val()!=''){
 		 				shuxin.push($(elem).val());
 		 			}
 		 		})
 		    	//js 判断是否没有选择属性的商品
 		    	var inputshuxinxuanze = document.querySelectorAll('input.shuxinxuanze');
 		    	for (var i = inputshuxinxuanze.length - 1; i >= 0; i--) {
 		    		var input  = inputshuxinxuanze[i];
 		    		var thisvalue = input.value;
 		    		console.log(thisvalue);
 		    		if(thisvalue==null || thisvalue==''){
 		    			return;
 		    		}
 		    	}
 		 		
 		 		let data =shuxin.join(',',shuxin);
 		 		
 		 		$.ajax({
 		 			url: url.goodproajax,
 		 			type: 'GET',
 		 			dataType: 'json',
 		 			data: {param: data,goodsid:goodsid},
 		 		})
 		 		.done(function(res) {
 		 			if(res.data.goodsspecdetail!=null){
 		 				$(".price").text(res.data.goodsspecdetail.price);
 		 				$("input[name=goodsprice]").val(res.data.goodsspecdetail.price);
 		 				$("input[name=goods_spec_detail_id]").val(res.data.goodsspecdetail.id);
 		 				
 		 			}
 		 			if(res.data.storegoodsstock!=null){
 		 				$(".kucun").text(res.data.storegoodsstock.stock);
 		 				$("input[name=store_goods_stock_id]").val(res.data.storegoodsstock.id);
 		 			}
 		 			// if(data.length==1){
 		 			// 	$('.kucun').text(data[0].stock);
 		 			// }
 		 			// if(data=='1'){
 		 			// 	alert("没有中商品");
 		 			// }
 		 			// specid = data[0].id;
 		 			// if(data.kuconu==0){
 		 			// 	alert("duibuqi meicunnj");
 		 			// }
 		 		 	// $('.price').text(data[0].price);
 		 		
 		 		})
 		 		.fail(function() {
 		 			//console.log("error");
 		 		})
 		 		.always(function() {
 		 			//console.log("complete");
 		 		});
 		 		
 		    })
 		 	
 	

 		 	/**
 		 	 * 立即购买
 		 	 */
		 	$('#orderprice').click(function(event) {

		 		let goods_spec_detail_id = $("input[name=goods_spec_detail_id]").val();
					let store_goods_stock_id = $("input[name=store_goods_stock_id]").val();
					let goods_id 			 = $("input[name=goods_id]").val();
					let sum 				 = parseInt($('input[name=sum]').val());
					let storeid 			 = $('input[name=storid]').val();
					let goods_price 		 = $('input[name=goodsprice]').val();
				let data ={
					storeid:storeid,
					sum:sum,
					goods_id:goods_id,
					goods_spec_detail_id:goods_spec_detail_id,
					store_goods_stock_id:store_goods_stock_id,
					goods_price:goods_price
				}

				let inputshuxinxuanze = document.querySelectorAll('input.shuxinxuanze');
 		    	for (var i = inputshuxinxuanze.length - 1; i >= 0; i--) {
 		    		let input  = inputshuxinxuanze[i];
 		    		let thisvalue = input.value;
 		    		console.log(thisvalue);
 		    		if(thisvalue==null || thisvalue==''){
 		    			alert("请选择"+$(inputshuxinxuanze[i]).data("shuxin"));
 		    			return;
 		    		}
 		    	}

		 		let num = $('.kucun').text();
		 		if(parseInt(num)<sum){
		 			alert("库存不够");
		 			return;
		 		}
		 		var params  = "?storeid="+data.storeid+"&sum="+data.sum
		 			params  +="&goods_id="+data.goods_id+"&goods_spec_detail_id="+data.goods_spec_detail_id
		 			params  +="&store_goods_stock_id="+data.store_goods_stock_id
		 			params  +="&goods_price="+data.goods_price
 		 		window.location.href=url.orderpre+params;

 		 	});

 		 	
 		 })
 		
 	</script>
{/block}

{block name="style"}
 <style type="text/css">
 	
 </style>
{/block}